var db = openDatabase('mydb', '1.0', 'WJN_DB', 2 * 1024 * 1024);

//加载主界面
$(function() {
	setTableView('select * from t_pc')
});

//查找
function search() {
	var type = $('#searchType').val();
	var txt = $('#searchTxt').val();
	var sql = "select * from t_pc where ";
	if(type == 'high' || type == 'low') { //只允许输入数字
		var reg = /^[0-9]+.?[0-9]*$/;
		if(!reg.test(txt)) {
			alert("请输入数字！");
			$('#searchTxt').val('');
			$('#searchTxt').focus();
			return;
		}
	}
	//判断
	if(type == 'name') {
		sql += " name like '%" + txt + "%'";
	} else if(type == 'branch') {
		sql += " branch like '%" + txt + "%'";
	} else if(type == 'title') {
		sql += " title like '%" + txt + "%'";
	} else if(type == 'high') {
		sql += " price > " + txt;
	} else if(type == 'low') {
		sql += " price < " + txt;
	}
	setTableView(sql);
}

//根据设置用户列表视图
function setTableView(sql) {
	db.transaction(function(tx) { //查看用户名是否存在
		tx.executeSql(sql, [], function(tx, results) {
			var len = results.rows.length,
				i;
			var htmltxt = '';
			for(i = 0; i < len; i++) {
				htmltxt += '<tr><td>' + results.rows.item(i).id + '</td>' +
					'<td>' + results.rows.item(i).name + '</td>' +
					'<td>' + results.rows.item(i).branch + '</td>' +
					'<td>' + results.rows.item(i).title + '</td>' +
					'<td>' + results.rows.item(i).price + '</td><td>' +
					'<a href="ad.html"><i class="fa fa-pencil"></i></a>' +
					'<a onclick="setid(' + results.rows.item(i).id + ');" href="#myModal" role="button" data-toggle="modal"><i class="fa fa-trash-o"></i></a></td></tr>';
			}
			$('#tb_pcs').html(htmltxt);
		}, null);
	});
}